package net.shop.action.front;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;

import ths.web.action.ActionContext;
import ths.web.action.ActionException;
import ths.web.action.Result;
import ths.web.action.result.ResultView;
import ths.web.annotation.HttpAction;
import ths.web.util.CookieUtils;
import ths.web.util.ParamUtils;

@HttpAction()
public class TestAction extends BaseFrontAction {

	@Override
	public Result execute() throws ActionException {
		ActionContext context = ActionContext.getContext();	
		HttpServletRequest request = context.getRequest();
		
		String parent = ParamUtils.getTrimParameter(request, "parent");
		String category = ParamUtils.getTrimParameter(request, "category");
		System.out.println("p:"+ parent +",c:"+ category);
		String template = "product_picture_pic";
		if (StringUtils.isEmpty(parent)) {
			template = "categories/"+ category;
		}
		
		String token = CookieUtils.getCookieValue(request, "login_token");
		System.out.println(token);		
		
		ResultView view = getViewResult(context);
		view.assign("username", "Jack");
		
		return view.toView(template);
	}

}
/*
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.shop.biz.service.ProductService;
import net.shop.common.LanguageKeyConstants;
import net.shop.dal.entity.Product;
import ths.web.action.ActionContext;
import ths.web.action.ActionException;
import ths.web.action.Result;
import ths.web.action.result.ResultView;
import ths.web.annotation.HttpAction;
import ths.web.i18n.Language;
import ths.web.session.Session;
import ths.web.session.SessionManagerHelper;
import ths.web.util.CookieUtils;
import ths.web.util.ParamUtils;

@HttpAction
public class ProductAction extends BaseFrontAction {

	@Resource
	private ProductService productService;
		
	public Result execute() throws ActionException {
		ActionContext context = ActionContext.getContext();	
		HttpServletRequest request = context.getRequest();
		HttpServletResponse response = context.getResponse();
		
		Session session = SessionManagerHelper.getSession(context);
		session.setAttribute("captcha", "tianhaishen");
		
		Language language = context.getLocaleInfo().getLanguage();
		String wellcome = language.getFormartMessage(LanguageKeyConstants.USER_LOGIN_WELLCOME, "TomJack");
		
		int productId = ParamUtils.getParameterInt(request, "pid", 0);
		if (0 == productId) {
			return getRedirectResult().jumpToHome();
		}
		
		CookieUtils.addGeneralSecurityCookie(response, "login_token", "programlife555@163.com");
		Product product = productService.getProductById(productId);
		
	
		
		ResultView view = getViewResult();
		view.assign("username", wellcome);
		view.assign("proudct", product);
		
		return view.toView("product_picture_list");
	}
}
*/
/*
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;

import net.shop.biz.service.ProductService;
import net.shop.dal.entity.Product;
import ths.web.action.ActionContext;
import ths.web.action.ActionException;
import ths.web.action.Result;
import ths.web.action.exception.ActionNotFoundException;
import ths.web.action.result.ResultView;
import ths.web.annotation.HttpAction;
import ths.web.util.CookieUtils;
import ths.web.util.ParamUtils;

@HttpAction
public class ProductDetailAction extends BaseFrontAction {

	@Resource
	private ProductService productService;
	
	@Override
	public Result execute() throws ActionException {
		ActionContext context = ActionContext.getContext();	
		HttpServletRequest request = context.getRequest();
		HttpServletResponse response = context.getResponse();
		
		String friendlyUrl = ParamUtils.getTrimParameter(request, "friendly_url");
		if (StringUtils.isEmpty(friendlyUrl)) {
			return getRedirectResult().jumpToHome();
		}

		CookieUtils.addGeneralSecurityCookie(response, "login_token", "programlife555@163.com");
		
		System.out.println(friendlyUrl);
		System.out.println(friendlyUrl.hashCode());
		
		Product product = productService.findProductByFriendlyUrlHaseCode(friendlyUrl.hashCode());
		if (product == null) {
			throw new ActionNotFoundException("Proudct not found.");
		}
		
		ResultView view = getViewResult();
		view.assign("username", "Jack");
		view.assign("proudct", product);
		
		return view.toView("product_detail");
	}
	
}
*/
